package com.motorwaymanage.impl;

import java.io.IOException;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.motorwaymanage.beans.ApplyInfo;
import com.motorwaymanage.biz.IApplyInfoBiz;
import com.motorwaymanage.biz.ILocationInfoBiz;
import com.motorwaymanage.dao.DBAccess;

public class ApplyInfoBizImpl implements IApplyInfoBiz{
	private DBAccess db=new DBAccess(); 
	@Override
	public List<ApplyInfo> selectApplyInfo(int mid) {
		SqlSession session=null;
		List<ApplyInfo> list=null;
		try {
			session=db.getSqlSession();
			IApplyInfoBiz applyInfoBiz=session.getMapper(IApplyInfoBiz.class);
			list=applyInfoBiz.selectApplyInfo(mid);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return list;
	}
	@Override
	public int addApplyInfo(ApplyInfo ai) {
		SqlSession session=null;
		int result=0;
		try {
			session=db.getSqlSession();
			IApplyInfoBiz applyInfoBiz=session.getMapper(IApplyInfoBiz.class);
			result=applyInfoBiz.addApplyInfo(ai);
			session.commit();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return result;
	}
	
	@Override
	public int updateAstatus(int aid) {
		SqlSession session=null;
		int result=0;
		try {
			session=db.getSqlSession();
			IApplyInfoBiz applyInfoBiz=session.getMapper(IApplyInfoBiz.class);
			result =applyInfoBiz.updateAstatus(aid);
			session.commit();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return result;
	}
	
	@Override
	public int updateAok(int aid) {
		SqlSession session=null;
		int result=0;
		try {
			session=db.getSqlSession();
			IApplyInfoBiz applyInfoBiz=session.getMapper(IApplyInfoBiz.class);
			result = applyInfoBiz.updateAok(aid);
			session.commit();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return result;
	}
	
	@Override
	public int delApplyInfo(int aid) {
		SqlSession session=null;
		int result=0;
		try {
			session=db.getSqlSession();
			IApplyInfoBiz applyInfoBiz=session.getMapper(IApplyInfoBiz.class);
			result=applyInfoBiz.delApplyInfo(aid);
			session.commit();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return result;
	}
	
	@Override
	public int modifyAokInfo(int aid, String laddr) {
		SqlSession session=null;
		int result = 0;
		try {
			session=db.getSqlSession();
			IApplyInfoBiz applyInfoBiz = session.getMapper(IApplyInfoBiz.class);
			int re1 = applyInfoBiz.updateAok(aid);
			if(re1 == 1) {
				ILocationInfoBiz locationInfoBiz = session.getMapper(ILocationInfoBiz.class);
				int re2 = locationInfoBiz.modifyAokInfo(laddr);
				if(re2 == 1) {
					session.commit();
					result = 1;
				} else {
					session.rollback();
				}
			} else {
				session.rollback();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return result;
	}
	
	@Override
	public int modifyAstInfo(int aid, String laddr) {
		SqlSession session=null;
		int result = 0;
		try {
			session=db.getSqlSession();
			IApplyInfoBiz applyInfoBiz = session.getMapper(IApplyInfoBiz.class);
			int re1 = applyInfoBiz.updateAstatus(aid);
			if(re1 == 1) {
				ILocationInfoBiz locationInfoBiz = session.getMapper(ILocationInfoBiz.class);
				int re2 = locationInfoBiz.modifyAstInfo(laddr);
				if(re2 == 1) {
					session.commit();
					result = 1;
				} else {
					session.rollback();
				}
			} else {
				session.rollback();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return result;
	}
}
